<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>2-11 填充图形</title>
</head>
<body>
  <canvas id="c" width="300" height="300" style="border: 1px solid #ccc;"></canvas>

  <script>
    const context = document.getElementById('c')
    const ctx = context.getContext('2d')

    ctx.moveTo(50, 50)
    ctx.lineTo(250, 50)
    ctx.lineTo(250, 250)
    ctx.lineTo(50, 250)
    ctx.closePath()
    

    // 非零环绕规则：
    // 顺时针 +1
    // 逆时针 -1
    // 结果为0时不填充
    ctx.moveTo(200, 100)
    ctx.lineTo(100, 100)
    ctx.lineTo(100, 200)
    ctx.lineTo(200, 200)
    ctx.closePath()
    ctx.fill()
  </script>
</body>
</html>